<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品类别</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="ID" width="100" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="180" align="center"></el-table-column>
      <!--      //<el-table-column prop="description" label="简介" header-align="center" :show-overflow-tooltip="true"></el-table-column>-->
      <el-table-column prop="sort" label="排序序号" width="180" align="center"></el-table-column>
      <el-table-column label="是否启用" width="180" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="handleChangeEnable(scope.row)"
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="子级类别" width="180" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="subsetsEnable(scope.row)">查看子集</el-button>
          <el-dialog
              title="二级分类"
              :visible.sync="dialogVisible"
              width="60%"
              :before-close="handleClose">



<!--二级分类-->
            <el-table :data="tableData1" border style="width: 100%">
              <el-table-column prop="id" label="ID" width="100" align="center"></el-table-column>
              <el-table-column prop="name" label="名称" width="180" align="center"></el-table-column>
              <el-table-column prop="sort" label="排序序号" width="180" align="center"></el-table-column>
              <el-table-column label="是否启用" width="180" align="center">
                <template slot-scope="scope">
                  <el-switch
                      @change="handleChangeEnable(scope.row)"
                      v-model="scope.row.enable"
                      :active-value="1"
                      :inactive-value="0"
                      active-color="#13ce66"
                      inactive-color="#999">
                  </el-switch>
                </template>
              </el-table-column>
              <el-table-column label="子级类别" width="180" align="center">
                <template slot-scope="scope">
                  <el-button type="text" @click="subsetsEnable1(scope.row)">查看子集</el-button>
                  <el-dialog
                      title="三级分类"
                      :visible.sync="dialogVisible1"
                      width="60%"
                      :before-close="handleClose">


<!--                    三级分类-->
                    <el-table :data="tableData2" border style="width: 100%">
                      <el-table-column prop="id" label="ID" width="100" align="center"></el-table-column>
                      <el-table-column prop="name" label="名称" width="180" align="center"></el-table-column>
                      <el-table-column prop="sort" label="排序序号" width="180" align="center"></el-table-column>
                      <el-table-column label="是否启用" width="180" align="center">
                        <template slot-scope="scope">
                          <el-switch
                              @change="handleChangeEnable(scope.row)"
                              v-model="scope.row.enable"
                              :active-value="1"
                              :inactive-value="0"
                              active-color="#13ce66"
                              inactive-color="#999">
                          </el-switch>
                        </template>
                      </el-table-column>
                      <el-table-column label="子级类别" width="180" align="center">
                        <span>暂无</span>
<!--                        <template slot-scope="scope">
                          <el-button type="text" @click="dialogVisible2 = true">查看子集</el-button>
                          <el-dialog
                              title="四级分类"
                              :visible.sync="dialogVisible2"
                              width="60%"
                              :before-close="handleClose">
                    <span slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="dialogVisible2 = false">关闭</el-button>
                    </span>
                          </el-dialog>
                        </template>-->
                      </el-table-column>
                      <el-table-column label="操作" width="180" align="center">
                        <template slot-scope="scope">
                          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                                     @click="openDeleteConfirm(scope.row)"></el-button>
                        </template>
                      </el-table-column>
                    </el-table>


                    <span slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="dialogVisible1 = false">关闭</el-button>
                    </span>
                  </el-dialog>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="180" align="center">
                <template slot-scope="scope">
                  <el-button type="danger" icon="el-icon-delete" size="mini" circle
                             @click="openDeleteConfirm(scope.row)"></el-button>
                </template>
              </el-table-column>
            </el-table>


            <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
  </span>
          </el-dialog>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>


</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      dialogVisible1: false,
      dialogVisible2: false,
      history: [],
      currentDepth: 1,
      currentParentId: 0,
      enableText: ['禁用', '启用'],
      displayText: ['不显示在导航栏', '显示在导航栏'],
      tableData: [],
      tableData1: [],
      tableData2: []

    }
  },
  methods: {
    subsetsEnable1(category){
      console.log("123123___________"+category.id);
      this.dialogVisible1 = true;
      let url = 'http://localhost:8080/category/list1/'+category.id;
      console.log('url = ' + url);
      this.axios
          .get(url).then((response) => {
        let responseBody = response.data;
        this.tableData2 = responseBody.data;
        console.log("接收到数据库穿过来的数据:" + this.tableData2);
      });
    },
    subsetsEnable(category) {
      console.log("123123___________"+category.id);
      this.dialogVisible = true;
      let url = 'http://localhost:8080/category/list1/'+category.id;
      console.log('url = ' + url);
      this.axios
          .get(url).then((response) => {
        let responseBody = response.data;
        this.tableData1 = responseBody.data;
        console.log("接收到数据库穿过来的数据:" + this.tableData1);
      });
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
    },
    goBack() {
      let parentCategory = this.history[--this.currentDepth - 1];
      this.history.pop();
      this.currentParentId = parentCategory.parentId;
      this.loadCategoryList();
    },
    handleChangeEnable(category) {
      let url = 'http://localhost:8080/category/' + category.id;
      if (category.enable == 1) {
        url += '/enable';
        console.log("--------------" + url);
      } else {
        url += '/disable';
        console.log("--------------" + url);
      }
      console.log('url = ' + url);
      this.axios
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '操作成功，已经将【' + category.name + '】的状态改为【' + this.enableText[category.enable] + '】！',
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
        this.loadCategoryList();
      });
    },
    handleChangeDisplay(category) {
      let url = 'http://localhost:9080/categories/' + category.id;
      if (category.isDisplay == 1) {
        url += '/display';
      } else {
        url += '/hidden';
      }
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '操作成功，已经将【' + category.name + '】的状态改为【' + this.displayText[category.isDisplay] + '】！',
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
        this.loadCategoryList();
      });
    },
    openDeleteConfirm(category) {
      let title = '提示';
      let message = '此操作将永久删除【' + category.name + '】类别，是否继续？';
      console.log("1234567890{}"+category.id)
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(category);


      }).catch(() => {
      });

    },
    handleDelete(category) {
      let url = 'http://localhost:8080/category/delete/' + category.id;
      console.log('url = ' + url);
      this.axios
          .post(url).then((response) => {
            let responseBody = response.data;
            console.log(responseBody.state);
        if (responseBody.state == 50100){
          this.$message.error(responseBody.message);
        }
        console.log("1231412asasd-----"+category.id)
        this.loadCategoryList();
      });
    },
    loadCategoryList() {
      let url = 'http://localhost:8080/category/list/0';
      console.log('url = ' + url);
      this.axios
          .get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
        console.log("接收到数据库穿过来的数据:" + responseBody.data);
        console.log("接收到数据库穿过来的数据:" + responseBody.data);
        console.log("接收到数据库穿过来的数据:" + this.tableData);
      });
    }
  },
  mounted() {
    this.loadCategoryList();
  }
}
</script>

<style scoped>

</style>